﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using ITAP.Core.Modelling.Primitives;
using ITAP.Core.Modelling;

namespace ITAP.ContourBuilder.Components.Dimensions
{
    [Serializable]
    public class ComponentLeaderDimension : ElementLeaderDimension
    {
        public override ContourElement Element
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public override bool IsComponentDotted
        {
            get
            {
                return true;
            }
        }

        public override ModelPoint Position
        {
            get
            {
                return component1.Position.Clone() as ModelPoint;
            }
        }

        private IContourComponent component1;

        public IContourComponent Component1
        {
            get { return component1; }
            set { component1 = value; }
        }
        private IContourComponent component2;

        public IContourComponent Component2
        {
            get { return component2; }
            set { component2 = value; }
        }

        public override void AcceptDimensionVisitor(IDimensionVisitor visitor)
        {
            visitor.Visit(this);
        }

        public override void Recalculate()
        {
            Line line = null;

            PointF downPoint;
            PointF upPoint;
            PointF leftPoint;
            PointF rightPoint;

            switch (this.LeaderType)
            {
                case LeaderDimensionType.Horizontal:
                    downPoint = new PointF((float)(this.component1.Position.X + this.component2.Position.X) / 2, (float)this.component1.Position.Y);
                    upPoint = new PointF((float)(this.component1.Position.X + this.component2.Position.X) / 2, (float)this.component2.Position.Y);
                    line = new Line(new ModelPoint(downPoint.X, downPoint.Y), new ModelPoint(upPoint.X, upPoint.Y));
                    break;
                case LeaderDimensionType.Vertical:
                    leftPoint = new PointF((float)this.component1.Position.X, (float)(this.component1.Position.Y + this.component2.Position.Y) / 2);
                    rightPoint = new PointF((float)this.component2.Position.X, (float)(this.component1.Position.Y + this.component2.Position.Y) / 2);
                    line = new Line(new ModelPoint(rightPoint.X, rightPoint.Y), new ModelPoint(leftPoint.X, leftPoint.Y));
                    break;
            }

            line = (Line)line.GetOffsetPrimitive(this.OffsetFromPrimitive);
            this.Primitive = line;
        }
    }
}
